<?php
//POST请求
class POST{
	
  var $ch=null;
  var $cookiefile="";
  var $HTTP_USER_AGENT="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
  var $refer="";
  var $call_c=0;
  
  
  function httpGet($url,$data){
    return $this->httpRequest("GET",$url,$data);
  }
  function httpPost($url,$data){
    return $this->httpRequest("POST",$url,$data);
  }
  
  function httpRequest($method, $url, $vars) {
  	 
  	if($this->call_c==0){
  		if($this->ch!=null){
  			//if($this->debug)print "DEBUG:curl_close()\n";
  			curl_close($this->ch);
  			unset($this->ch);
  		}
  		$this->ch=curl_init();
  	}
  	$ch = $this->ch; //
  	////////////////////////////////////////////////////////////
  	////////////////////////////////////////////////////////////
  	//if($this->header!=null){
  	//	//if($this->debug)print "DEBUG: CURLOPT_HTTPHEADER!!";//print_r($this->header);
  	//	curl_setopt($ch,CURLOPT_HTTPHEADER,$this->header);//TODO this is a array...
  	//	$this->header=null;
  	//}else{
  	curl_setopt($this->ch, CURLOPT_USERAGENT, $this->HTTP_USER_AGENT);
  	$HTTP_USER_AGENT=$_SERVER['HTTP_USER_AGENT'];
  	if($HTTP_USER_AGENT!="") curl_setopt($ch, CURLOPT_USERAGENT, $HTTP_USER_AGENT);
  	/*if(@$this->_flag_memcache){
  		$cookie_mem=$this->loadSessionVar("COOKIE");
  		$cookie=unserialize($cookie_mem);
  		if($this->debug) quicklog("DEBUG-netCommon","Load Mem Cookie:$cookie_mem  -- $url");
  		if ( count($cookie) > 0 ) {
  			//$cookie_str = "Cookie:\t";
  			foreach ( $cookie as $cookieKey => $cookieVal ) { $cookie_str .= $cookieKey."=".urlencode($cookieVal)."; "; }
  		}
  		curl_setopt($ch, CURLOPT_COOKIESESSION, false);
  		if($cookie_str != ""){
  			if($this->debug) quicklog("DEBUG-netCommon","Send:[$cookie_str]  -- $url");
  			curl_setopt($ch, CURLOPT_COOKIE, $cookie_str);
  		}
  	}else if($this->cookiefile!=""){*/
    		curl_setopt($ch, CURLOPT_COOKIEJAR,"saemc://".$this->cookiefile .'.txt');
  		curl_setopt($ch, CURLOPT_COOKIEFILE,"saemc://".$this->cookiefile .'.txt');
    	//}
  	if($this->refer!=""){
  		curl_setopt($ch, CURLOPT_REFERER, $this->refer);
  		$this->refer="";//clear
  	}else{
  		curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  	}
  	//}
  	//TODO
  	//CURLOPT_HTTPPROXYTUNNEL
  	//CURLOPT_PROXYAUTH,CURLAUTH_BASIC and CURLAUTH_NTLM
  	//CURLOPT_PROXY
  	//CURLOPT_PROXYPORT
  	//CURLOPT_PROXYTYPE Either CURLPROXY_HTTP (default) or CURLPROXY_SOCKS5.
  	//CURLOPT_PROXYUSERPWD;A username and password formatted as "[username]:[password]" to use for the connection to the proxy.
  	if ($method == "POST") {
  		curl_setopt($ch, CURLOPT_HTTPGET, false);
  		curl_setopt($ch, CURLOPT_POST, true);
  		curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
  	}else{
  		curl_setopt($ch, CURLOPT_POST, false);
  		curl_setopt($ch, CURLOPT_POSTFIELDS, false);
  		curl_setopt($ch, CURLOPT_HTTPGET, true);
  	}
  	////////////////////////////////////////////////////////////
  	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  	curl_setopt($ch, CURLOPT_VERBOSE, @$this->verbose);
  	////////////////////////////////////////////////////////////
  	curl_setopt($ch, CURLOPT_URL, $url);
  	$data = curl_exec($ch);
        Log::Write($url."--".$vars."--".var_export($data,true),"postnew.txt");
  	$info=curl_getinfo($ch);
  	if($info['http_code']==200){
  		if ($data) {
  			if (@$this->callback)
  			{
  				$callback = $this->callback;
  				$this->callback = false;
  				call_user_func($callback, $data);
  			} else {
  				$str=array(
  					0=>"号码不正确",
                                        1=>"注册成功",
                                        2=>"密令验证失败",
                                        3=>"用户名不能为空",
                                        4=>"呢称不能为空",
                                        5=>"密码不能为空",
                                        6=>"邮箱不能为空",
                                        7=>"邮箱格式不正确",
                                        8=>"该号码已被注册",
                                        9=>"该邮箱已注册过会员",
                                        10=>"昵称已被使用",
                                        11=>"设置密码成功",
                                        12=>"指令不正确",
                                        22=>"未知错误",
                                        51=>"网站未开通短信注册",
                                        55=>"网站未开通"
  				);
  				$body=$data;
  				
          if(isset($str[$body])){
            return $this->return_body = $str[$body];
          }else if(!is_numeric($body)){
            return $body."--注册接口版本太低!请更新!";
          }else{
            return $body;
          }

  				/*if(is_numeric($body) && isset($str[intval($body)])){
  					$this->return_body=$str[$body];
  				
  					return $str[$body];
  				}else if(!is_numeric($body)){
                                	return "注册接口版本太低!请更新!";
                                }else{
  					return $body;
  				}*/
  			}
   		 } else {
    		return curl_error($ch);
    	}
  	}else{
          //if(L($info['http_code'])==$info['http_code'])
  			return "调用注册接口时出现".$info['http_code']."错误";
          //else 
          //	return L($info['http_code']);
  	}
  }
  function __destruct(){
  		if ($this->ch!=null)curl_close($this->ch);
  	}
}